<?php
namespace QinglongApp\Playday\Src\Service;

use QinglongApp\Playday\Src\Model\PlaydayPlan as PlaydayList;
use QinglongApp\Playday\Src\Model\PlaydayBlacklist;
use QinglongApp\Playday\Src\Model\PlaydayInterfix;
use DB;
use QinglongApp\Playday\Src\Model\PlaydayPlanRsa;

class Playday {
    /**
     * 获取调休通知列表
     */
    public function getList() {
        return PlaydayList::get();
    }
    /**
     * 获取调休通知详情
     */
    public function getDetail($playday_id) {
        $result['playday_plan'] = PlaydayList::find($playday_id);
        $result['blacklist'] = PlaydayBlacklist::where('playday_plan_id', $playday_id)->get();
        $result['interfix'] = PlaydayInterfix::where('playday_plan_id', $playday_id)->get();
        return $result;
    }
    /**
     * 后台添加调休通知
     */
    public function playdayDeliver($playday_info) {
        $result = PlaydayList::create([
            'creator' => $playday_info['creator'],
            'content' => $playday_info['content'],
            'start' => $playday_info['start'],
            'end' => $playday_info['end'],
            'deadline' => $playday_info['deadline'],
            'allow_days' => $playday_info['allow_days'],
            'weekly_allow_days' => $playday_info['weekly_allow_days'],
        ]);

        if( !empty( $result)){
            foreach ($playday_info['blacklist'] as $tag) {
                $tag['playday_plan_id'] = $result['id'];
                PlaydayBlacklist::create($tag);
            }
            foreach ($playday_info['interfix'] as $tag_interfix) {
                $tag_interfix['playday_plan_id'] = $result['id'];
                PlaydayInterfix::create($tag_interfix);
            }

            $result = 1;//插入成功
        }
        else{
            $result = 0;//插入失败
        }

        return $result;
    }
    /**
     * 后台修改调休通知
     */
    public function playdayEdit($playday_info) {

        $result = PlaydayList::where('id',$playday_info['playday_id'])->update([
            'creator' => $playday_info['creator'],
            'content' => $playday_info['content'],
            'start' => $playday_info['start'],
            'end' => $playday_info['end'],
            'deadline' => $playday_info['deadline'],
            'allow_days' => $playday_info['allow_days'],
            'weekly_allow_days' => $playday_info['weekly_allow_days'],
        ]);

        if(!empty($result)){
            PlaydayBlacklist::where('playday_plan_id', $playday_info['playday_id'])->delete();
            if( !empty( $playday_info['blacklist'])){
                foreach ($playday_info['blacklist'] as $tag) {
                    $tag['playday_plan_id'] = $playday_info['playday_id'];
                    PlaydayBlacklist::create($tag);
                }
            }
            PlaydayInterfix::where('playday_plan_id', $playday_info['playday_id'])->delete();
            if( !empty( $playday_info['interfix'])){
                foreach ($playday_info['interfix'] as $tag_interfix) {
                    $tag_interfix['playday_plan_id'] = $playday_info['playday_id'];
                    PlaydayInterfix::create($tag_interfix);
                }
            }
        }
        return $result;
    }
    /**
     * 后台删除调休通知
     */
    public function playdayDel($playday_info) {

        $result = PlaydayList::where('id',$playday_info['playday_id'])->delete();

        if(!empty($result)){
            PlaydayBlacklist::where('playday_plan_id', $playday_info['playday_id'])->delete();
            PlaydayInterfix::where('playday_plan_id', $playday_info['playday_id'])->delete();
        }



        return $result;
    }
    /*
     * 获取禁休日列表
     */
    public function getBlacklist(){
        $result = PlaydayBlacklist::get();
        return $result;
    }

    /*
     * 获取禁休日详情
     */
    public function getBlackdetail($black_id){
        $result = PlaydayBlacklist::find($black_id);
        return $result;
    }
}